package gov.child.service.impl;

import gov.child.dao.CollectionDao;
import gov.child.entities.po.ECollection;
import gov.child.entities.po.EUser;
import gov.child.entities.vo.BeanTransformer;
import gov.child.entities.vo.Collection;
import gov.child.entities.vo.Page;
import gov.child.entities.vo.User;
import gov.child.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author fzh
 * @since 2017/9/14
 */
@Service("collectionService")
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionDao collectionDao;

    @Override
    public Collection findById(Integer id) {
        ECollection collection = collectionDao.findById(id);
        if (Objects.isNull(collection)) return Collection.EMPTY_COLLECTION;
        return Collection.parseFrom(collection);
    }

    @Override
    public List findByUser(User user) {
        EUser u = User.parseTo(user);
        return BeanTransformer.parseFrom(Collection.class, collectionDao.findByUser(u));
    }

    @Override
    public boolean save(Collection collection) {
        ECollection c = Collection.parseTo(collection);
        collectionDao.save(c);
        return true;
    }

    @Override
    public boolean delete(Collection collection) {
        ECollection c =Collection.parseTo(collection);
        collectionDao.delete(c);
        return true;
    }

    @Override
    public boolean update(Collection collection) {
        ECollection c = Collection.parseTo(collection);
        collectionDao.update(c);
        return true;
    }
}
